//
//  XMMeetEvaluateView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/23.
//

#import "XMMeetEvaluateView.h"
#import "XMConfirmOrderMeetCell.h"
#import "XMMeetEvaluateCell.h"

@interface XMMeetEvaluateView ()<UITableViewDelegate, UITableViewDataSource, UITextViewDelegate>

@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) XMConfirmOrderMeetCell *meetInfoView;

@property (nonatomic, strong) UITextView *explainTextView;
@property (nonatomic, strong) UILabel *placeHolderLabel;

@property (nonatomic, strong) XMOrderDetailModel *orderDetailModel;

@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *itemTitleArr;

@property (nonatomic, strong) NSMutableDictionary *selectDic;

@end

@implementation XMMeetEvaluateView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"待评价";
    self.backgroundColor = kColorHEX(0xF6F6F6);
    _titleArr = @[@"对方是否准时到场？", @"对方的性格和他的简介是否一致", @"对这次见面的感受"];
    _itemTitleArr = @[@[@"准时到场", @"虽迟但到", @"没有出现"], @[@"非常一致", @"大体符合", @"不太一样"], @[@"很满意", @"一般般", @"一般般"]];
    [self.selectDic safe_setObject:@"准时到场" forKey:@"0"];
    [self.selectDic safe_setObject:@"非常一致" forKey:@"1"];
    [self.selectDic safe_setObject:@"很满意" forKey:@"2"];
    
    [self addSubview:self.tbl];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(260.0))];
    headView.backgroundColor = kColorHEX(0xffffff);
    [headView addSubview:self.avatarImgView];
    [headView addSubview:self.nameLabel];
    UIView *meetInfoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(70.0), self.width, KScaleWidth(100.0))];
    [headView addSubview:meetInfoBgView];
    [meetInfoBgView addSubview:self.meetInfoView];
    [headView addSubview:self.priceLabel];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(206.0), self.width, KScaleWidth(53.0))];
    bottomView.backgroundColor = kColorHEX(0xF6F6F6);
    [headView addSubview:bottomView];
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, self.width - KScaleWidth(16.0), KScaleWidth(53.0)) font:KFontSize(17.0) textColor:kColorHEX(0x666666)];
    explainLabel.text = @"告诉我你们对这次买家的感受";
    [bottomView addSubview:explainLabel];
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(202.0))];
    footView.backgroundColor = kColorHEX(0xF6F6F6);
    [footView addSubview:self.explainTextView];
    [_explainTextView addSubview:self.placeHolderLabel];
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(137.0) - KScaleWidth(100.0), _explainTextView.maxY + KScaleWidth(24.0), KScaleWidth(100.0), KScaleWidth(38.0)) title:@"取消" titleColor:kColorHEX(0x333333)];
    [cancelBtn setRadius:cancelBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:cancelBtn];
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(16.0) - KScaleWidth(100.0), cancelBtn.y, KScaleWidth(100.0), KScaleWidth(38.0)) title:@"提交" titleColor:kColorHEX(0x333333)];
    [confirmBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [confirmBtn setCornerRadius:confirmBtn.height/2];
    [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:confirmBtn];
    _tbl.tableHeaderView = headView;
    _tbl.tableFooterView = footView;
}

- (void)setContentWithOrderDetailModel:(XMOrderDetailModel *)orderDetailModel {
    _orderDetailModel = orderDetailModel;
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:orderDetailModel.avatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _nameLabel.text = orderDetailModel.nickname;
    [self.meetInfoView setContentWithOrderDetailModel:orderDetailModel];
    _priceLabel.text = [NSString stringWithFormat:@"¥%@", orderDetailModel.price];
}

//取消
- (void)cancelAction:(UIButton *)button {
    
}

//提交
- (void)confirmAction:(UIButton *)button {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_setObject:[self.selectDic safeStringForKey:@"0"] forKey:@"is_timely"];
    [parameters safe_setObject:[self.selectDic safeStringForKey:@"1"] forKey:@"personality"];
    [parameters safe_setObject:[self.selectDic safeStringForKey:@"2"] forKey:@"feel"];
    [parameters safe_setObject:_explainTextView.text forKey:@"content"];
    [parameters safe_setObject:_orderDetailModel.serviceId forKey:@"serviceId"];
    self.confirmBlock ? self.confirmBlock(parameters) : nil;;
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return KScaleWidth(8.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(8.0))];
    footView.backgroundColor = kColorHEX(0xF6F6F6);
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(112.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMMeetEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMMeetEvaluateCellName" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setContentWithTitle:_titleArr[indexPath.section]
                      itemArr:_itemTitleArr[indexPath.section]];
    XMWeakSelf;
    cell.itemSelectBlock = ^(NSString * _Nonnull str) {
        [weakSelf.selectDic safe_setObject:str forKey:[NSString stringWithFormat:@"%ld", indexPath.section]];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH) style:UITableViewStylePlain];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.backgroundColor = kColorHEX(0xF6F6F6);
        [_tbl registerClass:[XMMeetEvaluateCell class] forCellReuseIdentifier:@"XMMeetEvaluateCellName"];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

#pragma mark - Init
- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(40.0), KScaleWidth(40.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(8.0), _avatarImgView.y, self.width - _avatarImgView.maxX - KScaleWidth(8.0), _avatarImgView.height) font:KFontSize(18.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (XMConfirmOrderMeetCell *)meetInfoView {
    if (!_meetInfoView) {
        _meetInfoView = [[XMConfirmOrderMeetCell alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(100.0))];
    }
    return _meetInfoView;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), self.meetInfoView.superview.maxY, self.width - KScaleWidth(16.0)*2, KScaleWidth(22.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentRight];
    }
    return _priceLabel;
}

- (UITextView *)explainTextView {
    if (!_explainTextView) {
        _explainTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(112.0))];
        _explainTextView.backgroundColor = kColorHEX(0xffffff);
        _explainTextView.font = KFontSize(14.0);
        _explainTextView.delegate = self;
        [_explainTextView setContentInset:UIEdgeInsetsMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(16.0))];
    }
    return _explainTextView;
}

- (UILabel *)placeHolderLabel {
    if (!_placeHolderLabel) {
        _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(5.0), 0, self.width - KScaleWidth(16.0)*2, KScaleWidth(32.0)) font:KFontSize(14.0) textColor:kColorHEX(0x999999)];
        _placeHolderLabel.text = @"还可以补充其他想说的话";
        _placeHolderLabel.userInteractionEnabled = NO;
    }
    return _placeHolderLabel;
}

- (NSMutableDictionary *)selectDic {
    if (!_selectDic) {
        _selectDic = [NSMutableDictionary dictionary];
    }
    return _selectDic;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
